<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\paper\command;
use think\admin\Controller;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
use app\paper\service\PaperTestService;

/**
 * 定时查询检测状态
 * Class Fans
 * @package app\wechat\command
 */
class PaperTest extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('paper:PaperTest')->setDescription('检测论文');
    }

    /**
     * 执行指令
     * @param Input $input
     * @param Output $output
     * @throws \think\admin\Exception
     */
    protected function execute(Input $input, Output $output)
    {
        ini_set('max_execution_time', 0);
        $this->app->log->error("定时查询检测状态及打包文档");
        $delist=$this->app->db->name('digit_detectionlist')->where([['iscode','<',4]])->select()->toArray();
        if($delist){
            foreach ($delist as $k=>$v){
                $d=[
                    'appid'=>1,
                    'taskid'=>$v['taskid']
                ];
                $url="http://49.233.250.248:8811/query/";
                $info = PaperTestService::instance()->send_post($url,$d);
                // $this->app->log->error($info);
                if(empty($info)){
                    $this->app->log->error("订单".$v['id']."未查询到检测结果");
                }else{
                    $info = json_decode($info,true);
                    if($info['result']=!1||$info['table']['state']!=2){
                       $this->app->log->error("订单".$v['id']."检测未完成,无需打包文档");
                    }else{
                        //生成检测报告并打包
                        $this->app->log->error("开始生成检测报告并打包");
                        $status = PaperTestService::instance()->getTestResult($v);
                        $this->app->log->error("生成检测报告并打包操作结果{$status}");
                        if($status){
                            $this->app->log->error("订单".$v['id']."检测报告打包成功压缩包地址:".$status);
                            $updata['iscode']=4;
                            $updata['zipurl']=$status;
                            if(isset($info['table']['similarity'])){
                                $updata['similarity']=$info['table']['similarity'];
                            }
                            $s=$this->app->db->name('digit_detectionlist')->where(['id'=>$v['id']])->update($updata);
                             $this->app->log->error("更新数据".$s);
                        }else{
                            $this->app->log->error("订单".$v['id']."检测报告打包失败:".$status);
                        }
                    }
                }
           
            }
        }else{
           $this->app->log->error("暂无需要查询检测结果的订单"); 
        }
        

    }

}